<template>
  <el-dialog :visible.sync="isShow" append-to-body
    title="字典明细编辑"
    width="400px"
    top="60px"
    :close-on-click-modal="false">
    <el-form ref="form" :model="fields" :rules="rules" label-width="80px">
      <el-form-item label="明细编码" prop="code">
        <el-input v-model.trim="fields.code" autocomplete="off" size="small" placeholder="请输入明细编码" maxlength="64" />
      </el-form-item>
      <el-form-item label="明细值" prop="value">
        <el-input v-model.trim="fields.value" autocomplete="off" size="small" placeholder="请输入明细值" maxlength="64" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :loading="loading" size="mini" @click="closePage"
        >取消</el-button
      >
       <el-button
        :loading="loading"
        type="primary"
        size="mini"
        @click="onOkBtnClick"
        >保存</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import { saveOrUpdate, get } from '@/api/detail'
export default {
  props: {
    value: {
      type: Object,
      default: () => Object.create({})
    }
  },
  data() {
    return {
      isShow: true,
      fields: {},
      parents: [],
      rules: {
        code: [{ required: true, message: '请输入明细编码', trigger: 'blur' }],
        value: [{ required: true, message: '请输入明细值', trigger: 'blur' }]

      }
    }
  },
  watch: {
    'isShow': function() {
      this.$emit('input', null)
    }
  },
  created() {
    this.get(this.value.id || null)
  },
  methods: {
    onOkBtnClick() {
      this.submitForm()
    },
    submitForm() {
      this.$refs['form'].validate((valid) => {
        this.fields.dicId = this.$parent.param.dicId
        valid && saveOrUpdate(this.fields).then(() => {
          this.isShow = false
          this.$parent.onSearchDetailBtnClick()
        }).catch(() => { })
      })
    },
    resetForm() {
      this.$refs['form'].resetFields()
    },
    async get(id) {
      const res = await get(id)
      this.fields = res
    },
    closePage() {
      this.isShow = false
      this.resetForm()
    },
  }
}
</script>
